/* file : readconfig.c 
 * read server config
 * all right reserved by neil duan 2009
 * create time 2009-2-3 11:26
 *
 */

#include "nd_common/nd_common.h"
#include "nd_common/nd_xml.h"
#include "nd_app/nd_app.h"
#define T_ERROR(msg) do {				\
	nd_msgbox("test error [%s]\n", msg) ;	\
	return -1 ;							\
} while(0)

NDINT32 read_config(NDINT8 *file, struct srv_config *readcfg)
{
	NDINT32 ret;
	NDINT8 *value ;
	ndxml *xml_sub, *xml_node, *xml_read ;
	ndxml_root xmlroot;
	
	nd_assert(file) ;
	ret = ndxml_load(file, &xmlroot) ;
	if(0!=ret) {
		T_ERROR("load xml from file") ;
	}
	xml_sub = ndxml_getnode(&xmlroot,"server_config") ;
	if(!xml_sub) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read server_config error") ;
	}
	
	xml_node = ndxml_refsub(xml_sub,"run_config") ;
	if(!xml_node) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config error") ;
	}

	//read logfile
	xml_read = ndxml_refsub(xml_node,"logfile") ;
	if(!xml_read) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config error") ;
	}
	value = ndxml_getval(xml_read) ;
	if(!value){
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config value error") ;
	}
	set_log_file(value) ;

	//read port
	xml_read = ndxml_refsub(xml_node,"port") ;
	if(!xml_read) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config error") ;
	}
	value = ndxml_getval(xml_read) ;
	if(!value){
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config value error") ;
	}
	readcfg->port = atoi(value) ;
	
	//read max connect	
	xml_read = ndxml_refsub(xml_node,"max_connect") ;
	if(!xml_read) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config error") ;
	}
	value = ndxml_getval(xml_read) ;
	if(!value){
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config value error") ;
	}
	readcfg->max_connect = atoi(value) ;

	//read listen mod
	xml_read = ndxml_refsub(xml_node,"listen_mod") ;
	if(!xml_read) {		
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config error") ;
	}
	value = ndxml_getval(xml_read) ;
	if(!value){
		ndxml_destroy(&xmlroot);
		T_ERROR("read run_config value error") ;
	}
	ndstrncpy(readcfg->listen_name,value, sizeof(readcfg->listen_name)) ;

	ndxml_destroy(&xmlroot);
	//readcfg->config_file = file ;
	return 0;

}
